//
//  NSDate+Format.m
//  MyUtils
//
//  Created by 唐可雄 on 16/8/15.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "NSDate+Format.h"

@implementation NSDate (Format)

#pragma mark - 部分

- (NSString *)descOfYearInChinese {
    // 定义农历数据
    NSArray * chYearArray = @[@"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉", @"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛己", @"壬午", @"癸未", @"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳", @"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸丑", @"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑", @"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥"];
    // 根据日期获取对应的农历
    NSCalendar * localCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    NSDateComponents *localComps = [localCalendar components:NSCalendarUnitYear fromDate:self];
    // 返回农历字符串
    return chYearArray[localComps.year-1];
}

- (NSString *)descOfMonthInChinese {
    // 定义农历数据
    NSArray *chMonthArray = @[@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月", @"十月", @"冬月", @"腊月"];
    // 根据日期获取对应的农历
    NSCalendar * localCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    NSDateComponents *localComps = [localCalendar components:NSCalendarUnitMonth fromDate:self];
    // 返回农历字符串
    return chMonthArray[localComps.month-1];
}

- (NSString *)descOfDayInChinese {
    // 定义农历数据
    NSArray *chDayArray = @[@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十"];
    // 根据日期获取对应的农历
    NSCalendar * localCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    NSDateComponents *localComps = [localCalendar components:NSCalendarUnitDay fromDate:self];
    // 返回农历字符串
    return chDayArray[localComps.month-1];
}

#pragma mark - 格式转换

+ (instancetype)dateFromStr1:(NSString *)str {
    NSRange start = [str rangeOfString:@"("];
    NSRange end = [str rangeOfString:@"+"];
    
    long long timeInterval = [[str substringWithRange:NSMakeRange(start.location+1, end.location-start.location-1)] longLongValue];
    return [NSDate dateWithTimeIntervalSince1970:timeInterval/1000.0];
}

- (NSString *)string1 {
    if (!self) {
        return @"";
    }
    return [NSString stringWithFormat:@"/Date(%lld+0800)/", (long long)([self timeIntervalSince1970]*1000)];
}

+ (NSDateFormatter *)dateFormatter2 {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    
    return dateFormatter;
}

+ (instancetype)dateFromStr2:(NSString *)str {
    return [[self dateFormatter2] dateFromString:str];
}

- (NSString *)string2 {
    return [[[self class] dateFormatter2] stringFromDate:self];
}

+ (NSDateFormatter *)dateFormatter3 {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    return dateFormatter;
}

+ (instancetype)dateFromStr3:(NSString *)str {
    return [[self dateFormatter3] dateFromString:str];
}

- (NSString *)string3 {
    return [[[self class] dateFormatter3] stringFromDate:self];
}

- (NSString *)string4 {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear ;
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    NSDateComponents *myCmps = [calendar components:unit fromDate:self];
    
    NSDateFormatter *dateFmt = [[ NSDateFormatter alloc] init];
    if (nowCmps.year != myCmps.year) {
        dateFmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    } else {
        if (nowCmps.day==myCmps.day) {
            dateFmt.dateFormat = @"今天 HH:mm:ss";
        } else if ((nowCmps.day-myCmps.day)==1) {
            dateFmt.dateFormat = @"昨天 HH:mm:ss";
        } else {
            dateFmt.dateFormat = @"MM-dd HH:mm:ss";
        }
    }
    
    return [dateFmt stringFromDate:self];
}

- (NSString *)string4_1 {
    NSTimeInterval timeInterval = -[self timeIntervalSinceNow];
    if (timeInterval < 60) {
        return @"1分钟内";
    } else if (timeInterval < 3600) {
        return [NSString stringWithFormat:@"%.f分钟前", timeInterval / 60.0];
    } else if (timeInterval < 86400) {
        return [NSString stringWithFormat:@"%.f小时", timeInterval / 3600.0];
    } else if (timeInterval < 2592000) {//within 30 days
        return [NSString stringWithFormat:@"%.f天前", timeInterval / 86400.0];
    } else if (timeInterval < 31536000) {//30 days to a year
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"M月d日";
        return [dateFormatter stringFromDate:self];
    } else {
        return [NSString stringWithFormat:@"%.f年前", timeInterval / 31536000.0];
    }
}

- (NSString *)string4_2 {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *theDay = [dateFormatter stringFromDate:self];
    NSString *currentDay = [dateFormatter stringFromDate:[NSDate date]];
    
    NSInteger timeInterval = -[self timeIntervalSinceNow];
    if (timeInterval < 60) {
        return @"1分钟内";
    } else if (timeInterval < 3600) {//within an hour
        return [NSString stringWithFormat:@"%.f分钟前", timeInterval / 60.0];
    } else if (timeInterval < 21600) {//within 6 hour
        return [NSString stringWithFormat:@"%.f小时前", timeInterval / 3600.0];
    } else if ([theDay isEqualToString:currentDay]) {//current day
        [dateFormatter setDateFormat:@"HH:mm"];
        return [NSString stringWithFormat:@"今天 %@", [dateFormatter stringFromDate:self]];
    } else if ([[dateFormatter dateFromString:currentDay] timeIntervalSinceDate:[dateFormatter dateFromString:theDay]] == 86400) {//one day ago
        [dateFormatter setDateFormat:@"HH:mm"];
        return [NSString stringWithFormat:@"昨天 %@", [dateFormatter stringFromDate:self]];
    } else {
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        return [dateFormatter stringFromDate:self];
    }
}

+ (NSDateFormatter *)dateFormatter5 {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    
    return dateFormatter;
}

+ (instancetype)dateFromStr5:(NSString *)str {
    return [[self dateFormatter5] dateFromString:str];
}

- (NSString *)string5 {
    return [[[self class] dateFormatter5] stringFromDate:self];
}

@end
